package Day18;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class View {
    private Scanner sc = new Scanner(System.in);
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    public String account(){
        System.out.print("请输入账号：");
        String s = sc.nextLine();
        return s;
    }

    public String password(){
        System.out.print("请输入密码：");
        String s = sc.nextLine();
        return s;
    }

    //菜单
    public int xxx(){
        System.out.println("请输入您需要的操作： 1-图书新增  2-图书修改  3-图书删除  4-图书查找  5-查看所有图书  0-退出");
        String s = sc.nextLine();
        int a = -1;
        try{
            a = Integer.parseInt(s);
        }catch (NumberFormatException e){
            System.out.println("输入有误请重新输入");
            xxx();
        }
        if(a<0 || a>5){
            System.out.println("输入有误，请重新输入");
            xxx();
        }
        return a;
    }

    //添加图书
    public Book add(){
        System.out.println("请输入书名：");
        String s = sc.nextLine();
        System.out.println("请输入价格:");
        String d = sc.nextLine();
        double m = 0.0;
        try{
            m = Double.parseDouble(s);
        }catch (NumberFormatException e){
            System.out.println("输入有误请重新输入");
            add();
        }
        System.out.println("请输入时间：(格式====2021-01-21)");
        String date = sc.nextLine();
        String time = null;
        try {
            Date time1 = format.parse(date);
            time = format.format(time1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Book b = new Book();
        b.setName(s);
        b.setMoney(m);
        b.setTime(time);
        return b;
    }

    //修改图书
    public Book reviseBook(){
        System.out.println("请输入新书名：");
        String name = sc.nextLine();
        System.out.println("请输入新价格：");
        String s1 = sc.nextLine();
        Double money = 0.00;
        try{
            money = Double.parseDouble(s1);
        }catch (NumberFormatException e){
            System.out.println("输入有误");
        }
        System.out.println("请输入新增时间：");
        String date = sc.nextLine();
        String time = null;
        try {
            Date time1 = format.parse(date);
            time = format.format(time1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Book b = new Book();
        b.setMoney(money);
        b.setName(name);
        b.setTime(time);
        return b;
    }

    //图书删除
    public String deleteBook(){
        System.out.println("请输入书名：");
        String s = sc.nextLine();
        return s;
    }

    //图书查找
    public String findBook(){
        System.out.println("请输入要查找的书名：");
        String s = sc.nextLine();
        return s;
    }
}
